import request from '@/utils/request'

// 查询字典数据列表
export function listData(query) {
  return request({
    url: '/system/dict/data/list',
    method: 'get',
    params: query
  })
}

// 查询字典数据详细
export function getData(dictCode) {
  return request({
    url: '/system/dict/data/' + dictCode,
    method: 'get'
  })
}

// 根据字典类型查询字典数据信息
export function getDicts(dictType) {
  return request({
    url: '/system/dict/data/type/' + dictType,
    method: 'get'
  })
}

// 新增字典数据
export function addData(data) {
  return request({
    url: '/system/dict/data',
    method: 'post',
    data: data
  })
}

// 修改字典数据
export function updateData(data) {
  return request({
    url: '/system/dict/data',
    method: 'put',
    data: data
  })
}

// 删除字典数据
export function delData(dictCode) {
  return request({
    url: '/system/dict/data/' + dictCode,
    method: 'delete'
  })
}



export function getNowYearMonth() {
  let date = new Date();
  let y = date.getFullYear();
  let m = date.getMonth() + 1;
   m = m < 10 ? "0" + m : m;
  return y + "-" + m ;


}
export function getNowYear() {
  let date = new Date();
  let y = date.getFullYear();
  return y;
}
export function getYearFirstDay() {
  let firstDay = new Date();
  firstDay.setDate(1);
  firstDay.setMonth(0);
  let tYear = firstDay.getFullYear();
  let tMonth = firstDay.getMonth()+1;
  let tDate = firstDay.getDate();
  return tYear + "-" + tMonth + "-" + tDate;
}

export function getNearDay(day) {
  let today = new Date();
  let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds); //注意，这行是关键代码
  let tYear = today.getFullYear();
  let tMonth = today.getMonth();
  let tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate = doHandleMonth(tDate);
  // console.log(tYear + "-" + tMonth + "-" + tDate);
  return tYear + "-" + tMonth + "-" + tDate;
}
export function doHandleMonth(month) {
  var m = month;
  if (month.toString().length == 1) {
    m = "0" + month;
  }
  return m;
}

export function numberToCurrencyNo(value) {
  if (!value) return value;
  // 获取整数部分
  const intPart = Math.trunc(value)
  // 整数部分处理，增加,
  const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
  // 预定义小数部分
  let floatPart = ''
  // 将数值截取为小数部分和整数部分
  const valueArray = value.toString().split('.')
  if (valueArray.length === 2) { // 有小数部分
    floatPart = valueArray[1].toString() // 取得小数部分
    if(floatPart>9||floatPart.length>1){
      return intPartFormat + '.' + floatPart
    }
    return intPartFormat + '.' + floatPart+'0';
  }else{
    floatPart='.00';
  }
  return intPartFormat + floatPart
}

export function numberToPercent(value) {
  if(null==value){
    return null;
  }
  let data = (value*100).toFixed(2)+'%'
  return data;
}




